package com.tencent.sr.iris.activity.interfaces.enums;

import lombok.AllArgsConstructor;
import lombok.Getter;

import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;
import java.util.Optional;

/**
 *
 */
@AllArgsConstructor
@Getter
public enum TaskEndStatusEnum {
    PROGRESS(0, "任务周期内"),
    PRE_END(1, "任务周期结束~T+60（包含T+60）"),
    END(2, "T+60~以后"),
    ;

    private final int code;
    private final String desc;

    public static TaskEndStatusEnum getByCode(int code) {
        for (TaskEndStatusEnum staff : TaskEndStatusEnum.values()) {
            if (staff.getCode() == code) {
                return staff;
            }
        }
        return null;
    }

    public static TaskEndStatusEnum getCodeByTaskEndTime(Date taskEndTime, Integer commissionTaskPlusDay) {
        Integer day = Optional.ofNullable(commissionTaskPlusDay).orElse(60);
        LocalDate now = LocalDate.now(ZoneId.systemDefault());
        LocalDate localTaskEndTime = taskEndTime.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
        LocalDate taskEndDatePlus60 = taskEndTime.toInstant().atZone(ZoneId.systemDefault())
                .toLocalDate().plusDays(day);

        LocalDate plusOne = localTaskEndTime.plusDays(1);
        if (plusOne.isAfter(now) || plusOne.equals(now)) {
            return PROGRESS;
        }

        if (taskEndDatePlus60.isEqual(now) || taskEndDatePlus60.isAfter(now)) {
            return PRE_END;
        }

        return END;
    }
}
